package admin_auth_server

import (
	"context"
	"google.golang.org/grpc"
	"jihe.common/protos/admin_auth_server/proto"
	"strings"
	"time"
)

var once = false
var conns []proto.AdminAuthClient

func GetClient() []proto.AdminAuthClient {
	if !once {
		for i := 0; i < len(conf.Get().Grpc.AdminAuthServers); i++ {
			grpcConn, err := grpc.Dial(conf.Get().Grpc.AdminAuthServers[i], grpc.WithInsecure())
			if err != nil {
				return conns
			}
			conns = append(conns, proto.NewAdminAuthClient(grpcConn))
		}
		once = true
	}
	return conns
}

func AdminAuthLogin(c context.Context, in *proto.AdminAuthUser) (out *proto.AdminAuthUser, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthLogin(c, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthCreateToken(ctx context.Context, in *proto.AdminAuthCreateTokenReq) (out *proto.AdminAuthCreateTokenRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthCreateToken(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthVerifyToken(ctx context.Context, in *proto.AdminAuthVerifyTokenReq) (out *proto.AdminAuthVerifyTokenRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthVerifyToken(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthGetUserRole(ctx context.Context, in *proto.AdminAuthGetUserRoleReq) (out *proto.AdminAuthUserRoles, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthGetUserRole(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthGetRoleByIds(ctx context.Context, in *proto.AdminAuthGetRoleByIdsReq) (out *proto.AdminAuthRoles, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthGetRoleByIds(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthLogout(ctx context.Context, in *proto.AdminAuthLogoutReq) (out *proto.AdminAuthLogoutRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthLogout(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthDisabledUser(ctx context.Context, in *proto.AdminAuthDisabledUserReq) (out *proto.AdminAuthDisabledUserRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthDisabledUser(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthCreateUser(ctx context.Context, in *proto.AdminAuthUser) (out *proto.AdminAuthUser, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthCreateUser(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthUpdateUser(ctx context.Context, in *proto.AdminAuthUser) (out *proto.AdminAuthUser, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthUpdateUser(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthResetPassword(ctx context.Context, in *proto.AdminAuthUser) (out *proto.AdminAuthUser, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthResetPassword(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthClearUserRole(ctx context.Context, in *proto.AdminAuthUser) (out *proto.AdminAuthUser, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthClearUserRole(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthGiveUserRole(ctx context.Context, in *proto.AdminAuthGiveUserRoleReq) (out *proto.AdminAuthUser, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthGiveUserRole(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthGetUserList(ctx context.Context, in *proto.AdminAuthUser) (out *proto.AdminAuthUsers, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthGetUserList(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthGetUserByIds(ctx context.Context, in *proto.AdminAuthGetUserByIdsReq) (out *proto.AdminAuthUsers, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthGetUserByIds(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthGetPermissionsByRoleId(ctx context.Context, in *proto.AdminAuthGetPermissionsByRoleIdReq) (out *proto.AdminAuthRolePermissions, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthGetPermissionsByRoleId(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthGetPermissionsByIds(ctx context.Context, in *proto.AdminAuthGetPermissionsByIdsReq) (out *proto.AdminAuthPermissions, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthGetPermissionsByIds(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthGetRoleList(ctx context.Context, in *proto.AdminAuthRole) (out *proto.AdminAuthRoles, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthGetRoleList(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthDeleteRole(ctx context.Context, in *proto.AdminAuthDeleteRoleReq) (out *proto.AdminAuthRole, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthDeleteRole(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthCreateRole(ctx context.Context, in *proto.AdminAuthRole) (out *proto.AdminAuthRole, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthCreateRole(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthUpdateRole(ctx context.Context, in *proto.AdminAuthRole) (out *proto.AdminAuthRole, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthUpdateRole(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthClearRolePermission(ctx context.Context, in *proto.AdminAuthClearRolePermissionReq) (out *proto.AdminAuthClearRolePermissionRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthClearRolePermission(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthGiveRolePermission(ctx context.Context, in *proto.AdminAuthGiveRolePermissionReq) (out *proto.AdminAuthGiveRolePermissionRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthGiveRolePermission(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func AdminAuthGetPermissionList(ctx context.Context, in *proto.AdminAuthGetPermissionListReq) (out *proto.AdminAuthPermissions, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].AdminAuthGetPermissionList(ctx, in)
		if err != nil && strings.Index(err.Error(), "connection refused") != -1 {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}
